package tr.com.netgis;

import tr.com.netgis.service.LocationListenerPolicy;
import tr.com.netgis.service.VariableLocationListenerPolicy;
import tr.com.netgis.util.Constants;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * 
 * Singleton
 * @author NetGis
 * 
 */
public class SharedPrefs {
	
	private static final String TAG = "NetMobileSharedPrefs";
	private static SharedPrefs INSTANCE; //Singleton instance
	public static String KEY_SEND_FREQUENCY = "sendFrequencyPref";
	public static String KEY_LOCATION_SIZE = "locationSizePref";
	public static String KEY_MIN_DISTANCE = "minDistancePref";
	public static String KEY_MIN_TIME = "minTimePref";
	public static String KEY_USE_GPS = "useGPSPref";
	public static String KEY_PASSWORD = "passwordPref";
	public static String KEY_USER_NAME = "userNamePref";
	public static String KEY_SESSION_ID = "sessionIdPref";
	public static final String EULA_PREFERENCE_KEY = "mts.tos_accepted";
	
	private static boolean inited = false;

	private int locationSize;
	private long minTime;
	private float minDistance;
	
	private String userName;
	private String password;
	private String sessionId;
	
	/**
	 * period
	 */
	private long sendFrequency;
	private boolean sendToServer;
	private boolean useGPS;
	private boolean eulaValue;
	private String serverUrl;

	/**
	 * 
	 * @return Web servis url
	 */
	public String getServerUrl(){
		return serverUrl;
	}
	
	/**
	 * Pref is gather as minute so that this value must be multiple with 60000
	 * @return as miliseconds
	 */
	public long getSendFrequency() {
		return sendFrequency * 1000L;
	}
	
	public String getSendFrequencyView() {
		return Float.toString((float)sendFrequency/60) + " dk";
	}

	public boolean isSendToServer() {
		return sendToServer;
	}
	
	public boolean useGPS() {
		return useGPS;
	}

	private Context ctx;
	
	private SharedPrefs() {
		this.ctx = NGApplication.getAppContext();
		getPrefs();
	}

	public static SharedPrefs getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new SharedPrefs();
			INSTANCE.init();
		} else {
			INSTANCE.refresh();			
		}
		return INSTANCE;
	}

	/**
	 * miliseconds
	 * 
	 * @return
	 */
	public long getMinTime() {
		return minTime * 1000;
	}
	
	public String getMinTimeView() {
		return minTime + " sn";
	}

	/**
	 * meters
	 * 
	 * @return
	 */
	public float getMinDistance() {
		return minDistance;
	}
	
	public String getMinDistanceView() {
		return minDistance + " m";
	}

	private void init (){
		if (inited) {
			return;
		}
		//write inititialization methods here
		inited = true;
	}
	
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
	}
	
	/**
	 * must be called after the prefs are changed
	 */
	public synchronized void refresh(){
		this.getPrefs();
	}

	/**
	 * xml/preferences.xml
	 */
	private void getPrefs() {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		sendToServer = prefs.getBoolean("sendToServer", true);
		useGPS = prefs.getBoolean(KEY_USE_GPS, false);
		eulaValue = prefs.getBoolean(EULA_PREFERENCE_KEY, false);
		
		serverUrl = prefs.getString("txtServerUrl", Constants.SERVER_URL);
		
		String sSendFrequency = prefs.getString(KEY_SEND_FREQUENCY, Long.toString(Constants.SEND_FREQUENCY));
		try {
			float value = Float.parseFloat(sSendFrequency);
			sendFrequency = new Float(value * 60).longValue(); //convert to seconds
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			sendFrequency = Constants.SEND_FREQUENCY;
		}
		String sMinTime = prefs.getString(KEY_MIN_TIME, Long.toString(Constants.MINIMUM_TIME_BETWEEN_UPDATES));
		try {
			minTime = Long.parseLong(sMinTime);
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			minTime = Constants.MINIMUM_TIME_BETWEEN_UPDATES;
		}
		String sMinDistance = prefs.getString(KEY_MIN_DISTANCE, Float.toString(Constants.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES));
		try {
			minDistance = Float.parseFloat(sMinDistance);
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			minDistance = Constants.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES;
		}
		
		
		String sLocationSize = prefs.getString(KEY_LOCATION_SIZE, Integer.toString(Constants.LOCATION_SIZE));
		try {
			locationSize = Integer.parseInt(sLocationSize);
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			locationSize = Constants.LOCATION_SIZE;
		}
		
		userName = prefs.getString(KEY_USER_NAME, "");
		password = prefs.getString(KEY_PASSWORD, "");
		sessionId = prefs.getString(KEY_SESSION_ID, "");
		
	}

	/**
	 * 
	 * @return At most how many location will be inserted in database incase of no internet connection.

	 */
	public int getLocationSize() {
		return locationSize;
	}

	public void setLocationSize(int locationSize) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_LOCATION_SIZE, Integer.toString(locationSize));
		editor.commit();
		refresh();
	}

	public void setSendFrequency(String sendFrequency) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_SEND_FREQUENCY, sendFrequency);
		editor.commit();
		refresh();
	}
	
	public void setMinDistance(int minDistance) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_MIN_DISTANCE, Integer.toString(minDistance));
		editor.commit();
		refresh();
	}

	public void setMinTime(int minTime) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_MIN_TIME, Integer.toString(minTime));
		editor.commit();
		refresh();
		FileLogger.log(KEY_MIN_TIME + " changed to " + minTime);
	}
	
	public String getUserName(){
		return userName;
	}
	
	public String getPassword(){
		return password;
	}
	
	public void setUserName(String userName) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_USER_NAME, userName);
		editor.commit();
		refresh();
	}
	
	public void setPassword(String password) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_PASSWORD, password);
		editor.commit();
		refresh();
	}

	public String getSessionId() {
		return sessionId;
	}

	public synchronized void setSessionId(String sessionId) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_SESSION_ID, sessionId);
		editor.commit();
		refresh();
	}
	
	public static LocationListenerPolicy getPolicy() {
		SharedPrefs prefs = SharedPrefs.getInstance();
		VariableLocationListenerPolicy policy = new VariableLocationListenerPolicy();
		policy.setMinDistance(prefs.getMinDistance());
		policy.setMinTime(prefs.getMinTime());
		return policy;
	}

}
